#ifndef CTP_ORDER_LINK_H_
#define CTP_ORDER_LINK_H_

#include "orderlink.h"
#include "THostFtdcTraderApi.h"
#include "AdaptorDlgBase.h"
#include "CtpLinkBase.h"
#include "config.h"

struct CtpOrderID {
	int	OrderRef;
	int SessionID;
	int FrontID;
};

struct CtpOrder : public AdaptorOrder {
	CtpOrderID	OrderID;
};


typedef pair<int, CtpOrder*> IDCtpOrderPair;

class CtpOrderLink :
	public OrderLink, public CThostFtdcTraderSpi, public CtpLinkBase {
public:
	CtpOrderLink(const string &name, CThostFtdcTraderApi *api);
	~CtpOrderLink();

	virtual bool sendOrder(AdaptorOrder *adaptorOrder);
	virtual void OnRspOrderInsert(CThostFtdcInputOrderField *order, 
		CThostFtdcRspInfoField *rspInfo, int nRsqID, bool isLast);
	virtual void OnRtnOrder(CThostFtdcOrderField *order);
	virtual void OnRtnTrade(CThostFtdcTradeField *trade);

	virtual bool cancelOrder(AdaptorOrder *adaptorOrder, bool isCxl);
	virtual void OnRspOrderAction(CThostFtdcInputOrderActionField *action,
		CThostFtdcRspInfoField *rspInfo, int nRsqID, bool isLast);

	//Override
	virtual void Connect();

	// Overrride
	virtual void Disconnect();

	virtual void OnFrontConnected();
	virtual void OnFrontDisconnected();
	virtual void OnRspUserLogin(CThostFtdcRspUserLoginField *rspLogin,
		CThostFtdcRspInfoField *rspInfo, int reqID, bool isLast);
	virtual void OnRspUserLogout(CThostFtdcUserLogoutField *userLogout, 
		CThostFtdcRspInfoField *rspInfo, int reqID, bool isLast);
	virtual void OnRspQrySettlementInfo(CThostFtdcSettlementInfoField *settlementInfo, 
		CThostFtdcRspInfoField *rspInfo, int requestID,bool isLast);
	virtual void OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *settlementInfo, 
		CThostFtdcRspInfoField *rspInfo, int requestID,bool isLast);
	virtual void OnRspError(CThostFtdcRspInfoField *rspInfo, int requestID, bool isLast);
	virtual void OnRspQryTrade(CThostFtdcTradeField *trade, CThostFtdcRspInfoField *rspInfo,
		int requestID, bool isLast);

	virtual void OnRspQrySettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *pSettlementInfoConfirm, 
		CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);

	virtual void OnRspQryOrder(CThostFtdcOrderField *pOrder, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);

	virtual AdaptorOrder *newOrder() {return new CtpOrder();}

private:
	void subscribeTrades();
	void subscribeOrders();
	void confirmSettlementInfo();
	void requestSettlementInfo();
	void requestSettlementInfoConfirm();
	void sendTestOrder();
	void cancelOrder(long orderRef, int frontID, int sessionID, bool isCxl);
	void cancelOrder(CThostFtdcOrderField *order);
	void cancelAll();
	CtpOrder* lookupCtpOrder(int orderRef);
private:
	CThostFtdcTraderApi		*mTraderApi;
	int						mRequestID;
	long					mOrderRefID;
	int						mFrontID;
	int						mSessionID;

	/* map order ref to ctporder */
	map<int, CtpOrder*>		mCtpOrderMap;

	int						mTradeRetrived;
};

#endif //CTP_ORDER_LINK_H_